﻿using System;
using System.Collections.Generic;
using System.Text;
using PacMan3D.Utils;
using Tao.OpenGl;
using Tao.DevIl;

namespace PacMan3D.Objects
{
    public class Font
    {
        private int TextureId;
        private int TextureIdMask;

        private Font(string img, string imgMask)
        {
            this.TextureId = ResourceLoader.LoadResource(img);
            this.TextureIdMask = ResourceLoader.LoadResource(imgMask);
        }

        public static void DrawNumber(int number, Point3D position)
        {
            float inc = 15.0f;
            foreach (char c in number.ToString())
            {
                switch (c)
                {
                    case '0':
                        Zero.DrawNumber(position);
                        break;

                    case '1':
                        One.DrawNumber(position);
                        break;

                    case '2':
                        Two.DrawNumber(position);
                        break;

                    case '3':
                        Three.DrawNumber(position); 
                        break;

                    case '4':
                        Four.DrawNumber(position); 
                        break;

                    case '5':
                        Five.DrawNumber(position);
                        break;

                    case '6':
                        Six.DrawNumber(position);
                        break;

                    case '7':
                        Seven.DrawNumber(position); 
                        break;

                    case '8':
                        Height.DrawNumber(position); 
                        break;

                    case '9':
                        Nine.DrawNumber(position); 
                        break;

                    default:
                        break;
                }

                position.X += inc; 
            }
        }

        private void DrawNumber(Point3D position)
        {
            Gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

            Gl.glPushMatrix();
            Gl.glTranslatef(position.X, position.Y, position.Z);

            Gl.glEnable(Gl.GL_TEXTURE_2D);
            Gl.glBindTexture(Gl.GL_TEXTURE_2D, this.TextureIdMask);
            Gl.glTexEnvf(Gl.GL_TEXTURE_ENV, Gl.GL_TEXTURE_ENV_MODE, Gl.GL_DECAL);
            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
            
            Gl.glEnable(Gl.GL_BLEND);
            Gl.glBlendFunc(Gl.GL_DST_COLOR, Gl.GL_ZERO);
            
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex2f(0.0f, 0.0f);
            Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex2f(0.0f, 30.0f);
            Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex2f(20.0f, 30.0f);
            Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex2f(20.0f, 0.0f);
            Gl.glEnd();

            Gl.glBindTexture(Gl.GL_TEXTURE_2D, this.TextureId);
            Gl.glBlendFunc(Gl.GL_ONE, Gl.GL_ONE);
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex2f(0.0f, 0.0f);
            Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex2f(0.0f, 30.0f);
            Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex2f(20.0f, 30.0f);
            Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex2f(20.0f, 0.0f);
            Gl.glEnd();

            Gl.glDisable(Gl.GL_BLEND);
            Gl.glDisable(Gl.GL_TEXTURE_2D);
            Gl.glPopMatrix();
        }

        private static Font Zero = new Font(Constants.RES_TEXT_ZERO, Constants.RES_TEXT_ZERO_MASK);
        private static Font One = new Font(Constants.RES_TEXT_ONE, Constants.RES_TEXT_ONE_MASK);
        private static Font Two = new Font(Constants.RES_TEXT_TWO, Constants.RES_TEXT_TWO_MASK);
        private static Font Three = new Font(Constants.RES_TEXT_THREE, Constants.RES_TEXT_THREE_MASK);
        private static Font Four = new Font(Constants.RES_TEXT_FOUR, Constants.RES_TEXT_FOUR_MASK);
        private static Font Five = new Font(Constants.RES_TEXT_FIVE, Constants.RES_TEXT_FIVE_MASK);
        private static Font Six = new Font(Constants.RES_TEXT_SIX, Constants.RES_TEXT_SIX_MASK);
        private static Font Seven = new Font(Constants.RES_TEXT_SEVEN, Constants.RES_TEXT_SEVEN_MASK);
        private static Font Height = new Font(Constants.RES_TEXT_HEIGHT, Constants.RES_TEXT_HEIGHT_MASK);
        private static Font Nine = new Font(Constants.RES_TEXT_NINE, Constants.RES_TEXT_NINE_MASK);
    }
}

